<template>
  <div class="blog-category">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>分类管理</span>
            <div class="btn-list">
              <el-button size="mini" @click="handleAdd" v-hasPermission="permission.add">新增</el-button>
            </div>
          </div>
          <div class="home-body">
            <el-table
              :data="tableData"
              style="width: 100%;margin-bottom: 20px;"
              row-key="id"
              border
              default-expand-all
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
              <el-table-column prop="category" label="分类名称" width="180"></el-table-column>
              <el-table-column prop="status" label="类型">
                <template slot-scope="scope">
                  <span>{{ scope.row.status == 1 ? '显示' : '隐藏' }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="sort" label="排序号"></el-table-column>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" label="操作" align="center" header-align="center" width="160">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleEdit(scope.row)" v-hasPermission="permission.edit">修改</el-button>
                  <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermission="permission.delete" v-if="!(scope.row.children&&scope.row.children.length > 0)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑弹窗 -->
    <category-edit
      @closeDialog="closeDialog('edit')"
      @confirmDialog="confirmDialog('edit')"
      @handleClose="closeDialog('edit')"
      v-if="dialogCategoryEdit"
      :dialogVisible="dialogCategoryEdit">
    </category-edit>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import cvueTreeGrid from '@/components/cvue-tree-grid'
import categoryEdit from './categoryEdit'
import {getClientHeight} from '@/util/tool'
export default {
  name: 'blog-category',
  components: {
    cvueTreeGrid,
    categoryEdit
  },
  data () {
    return {
      dialogCategoryEdit: false,
      searchValue: '',
      actionType: 'add',
      rowData: {},
      tableData: []
    }
  },
  computed: {
    height () {
      var height = getClientHeight() - 170
      return height + 'px'
    },
    ...mapGetters(['token', 'tenantId'])
  },
  mounted () {
    this.handleList()
  },
  methods: {
    // 新增
    handleAdd () {
      this.actionType = 'add'
      this.dialogCategoryEdit = true
    },
    // 编辑
    handleEdit (row) {
      this.actionType = 'edit'
      this.dialogCategoryEdit = true
      this.rowData = row
    },
    // 删除
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        this.tableLoading = true
        this.$store.dispatch('blog/BlogCategoryDelete', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.handleList()
          this.tableLoading = true
        }).catch((err) => {
          console.log(err)
          this.tableLoading = true
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    // 列表接口
    handleList () {
      this.tableLoading = true
      var params = {
        status: ''
      }
      this.$store.dispatch('blog/BlogCategoryGet', params).then(res => {
        if (res.code == 200) {
          this.tableData = res.data
          this.tableData = this.getTree(res.data)
        } else {
          this.tableData = []
        }
        this.tableLoading = false
      }).catch(err => {
        this.$message({type: 'error', message: err.msg})
        this.tableLoading = false
      })
    },
    // 遍历生成树
    getTree (treeData) {
      for (var i = 0; i < treeData.length; i++) {
        treeData[i].children = []
        for (var j = 0; j < treeData.length; j++) {
          if (treeData[i].id == treeData[j].pid) {
            treeData[i].children.push(treeData[j])
          }
        }
      }
      var arr = []
      for (i = 0; i < treeData.length; i++) {
        if (treeData[i].pid == '0') {
          arr.push(treeData[i])
        }
      }
      return arr
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogCategoryEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogCategoryEdit = false
        this.handleList()
      }
    }
  }
}
</script>

<style lang="scss">
.blog-category {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
